package com.lovegu.collection.list;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 老顾
 * @title: ArrayListTest
 * @projectName java-restudy
 * @email: 1437594522@qq.com
 * @date 2022/9/24 11:20
 */
public class ArrayListTest {
    @Test
    public void testArrayListAdd(){
        List<Integer> list = new ArrayList<>();
        Long start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            list.add((int)(Math.random()*100));
        }
        Long end = System.currentTimeMillis();
        System.out.printf("用时%d毫秒。",end-start);
    }
    @Test
    public void testLinkedListAdd(){
        List<Integer> list = new LinkedList<>();
        Long start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            list.add((int)(Math.random()*100));
        }
        Long end = System.currentTimeMillis();
        System.out.printf("用时%d毫秒。",end-start);
    }
    @Test
    public void testArrayListFor(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            list.add((int)(Math.random()*100));
        }
        System.out.println("开始------");
        Long start = System.currentTimeMillis();
        for (int i = 0; i < list.size(); i++) {
            list.get(i);
        }
        Long end = System.currentTimeMillis();
        System.out.printf("用时%d毫秒。",end-start);
    }
    @Test
    public void testArrayListIterator(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            list.add((int)(Math.random()*100));
        }
        System.out.println("开始------");
        Long start = System.currentTimeMillis();
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()){
            iterator.next();
        }
        Long end = System.currentTimeMillis();
        System.out.printf("用时%d毫秒。",end-start);
    }
    @Test
    public void testLinkedListIterator(){
        List<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10000000; i++) {
            list.add((int)(Math.random()*100));
        }
        System.out.println("开始------");
        Long start = System.currentTimeMillis();
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()){
            iterator.next();
        }
        Long end = System.currentTimeMillis();
        System.out.printf("用时%d毫秒。",end-start);
    }
}
